/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Whitemice.ZOGI;

namespace Whitemice.ZOGI.GtkWidgets
{
  [System.ComponentModel.ToolboxItem(true)]
	public class ContactNodeView : CompactNodeView
	{
    public EntityEventHandler OnContactEdit;
		public EntityEventHandler OnContactRemove;
		public EntityEventHandler OnContactUnassign;
		
		/* @brief Constructor */
		public ContactNodeView() 
			: base(null, false, new Gtk.NodeStore(typeof(ContactListNode)))
		{ SetupColumns(); }
	
		/* @brief Constructor */
		public ContactNodeView(Label status, bool popup) 
			: base(status, popup, new Gtk.NodeStore(typeof(ContactListNode)))
		{
			SetupColumns();			
		} /* End Constructor */
		
		public void SetupColumns()
		{
			AppendTextColumn("Id", 0, true, false, true);
			AppendTextColumn("Name", 1, true, true, true);
			AppendTextColumn("Title", 2, true, true, true);
			AppendTextColumn("E-Mail", 3, true, true, true);
			NodeStore.Clear();
			ButtonPressEvent += new ButtonPressEventHandler(OnButonPressEvent);
			RowActivated += new RowActivatedHandler(OnRowActivated);
		}
		
		/* @brief Clear the contents of the view */
		public void Clear()
		{
      Registry.UnregisterForNotification(this);
      this.NodeStore.Clear();
    }
		
		/* @brief Load a new set of data into the list */
		public override void Fill(IEnumerable<Entity> _data, string _message)
		{
			this.NodeStore.Clear();
			foreach(Entity entity in _data)
			{
			  if (entity is Contact)
			  {
          Registry.RegisterForNotification(entity, this);
				  this.NodeStore.AddNode(new ContactListNode(entity as Contact));
				  //entity.OnEntityDeleted += new zOGIEntityChangeHandler(Remove);
				}
			}
			if ((_message != null) && (statusLabel != null))
				statusLabel.Text = _message;		
		} /* End Fill */
		
		/* @brief Process edit event */
		public void ContactEdit(object _sender, EventArgs _args)
		{ 
			OnContactEdit(
				(_sender as PopUpMenuItem).ContextObject,
				"edit",
				null); 
		}
		
		/* @brief Process removal event */
		public void ContactRemove(object _sender, EventArgs _args)
		{ 
			OnContactRemove(
				(_sender as PopUpMenuItem).ContextObject,
				"remove",
				null); 
		}
		
		public void ContactUnassign(object _sender, EventArgs _args)
		{ 
			OnContactUnassign(
				(_sender as PopUpMenuItem).ContextObject,
				"unassign",
				null); 
		}		
		
		[GLib.ConnectBefore]
		protected void OnRowActivated(object sender, Gtk.RowActivatedArgs args)
		{
		 	ITreeNode				node;

			node = (sender as NodeView).NodeSelection.SelectedNode;
			if ((node is ContactListNode) && (this.OnContactEdit != null))
	  	  OnContactEdit(((ContactListNode)node).Contact, "edit", null);
		}		
		
		/* @brief Handler for click events on the list */
		[GLib.ConnectBefore]		
		public void OnButonPressEvent(object _sender, ButtonPressEventArgs _args)
		{
			ITreeNode				node;
			PopUpMenuItem 	menuItem;
			Contact					contact;

			/* Bail out if popup menu is not enabled */
			if(!(PopupEnabled))
		  {
				return;
		  }
			/* Get the selected list node from the nodeview
				 and bail out if result is not a ContactListNode */
			node = (_sender as NodeView).NodeSelection.SelectedNode;
			if (node is ContactListNode)
				contact = ((ContactListNode)node).Contact;
				else return;
			/* Is the click a right-button */
			if (_args.Event.Button == 3) 
			{
				Menu popupMenu = new Menu ();
				/* Add edit/view menu item if there is a handler */
				if (OnContactEdit != null)
				{
					if ((contact.CanModify))
						menuItem = new PopUpMenuItem(contact, "Edit");
					else
						menuItem = new PopUpMenuItem(contact, "View");
					menuItem.Activated += new EventHandler(ContactEdit);
					menuItem.Sensitive = true;
					popupMenu.Append (menuItem);
				} /* End-If there is an edit handler */
				if (popupMenu.Children.Length > 0)
				  popupMenu.Append (new SeparatorMenuItem ());
				/* Add remove menu item if there is a handler */
				if (OnContactRemove != null)
				{
					menuItem = new PopUpMenuItem (contact, "Delete");
					menuItem.Activated += new EventHandler(ContactRemove);
					if ((contact.CanModify))
						menuItem.Sensitive = true;
					else
						menuItem.Sensitive = false;
					popupMenu.Append (menuItem);
  			} /* End-If there is a delete handler */
				if (popupMenu.Children.Length > 0)
				  popupMenu.Append (new SeparatorMenuItem ());  			
				if (OnContactUnassign != null)
				{
					menuItem = new PopUpMenuItem (contact, "Unassign");
					menuItem.Activated += new EventHandler(ContactUnassign);
					if ((contact.CanModify))
						menuItem.Sensitive = true;
					else
						menuItem.Sensitive = false;
					popupMenu.Append (menuItem);				  
				}
				/* Show the menu only if there are menu items */
        if (popupMenu.Children.Length > 0)
				{
					popupMenu.Popup (null, null, null, 3, Gtk.Global.CurrentEventTime);
					popupMenu.ShowAll();
				} /* End-If there are handlers, show menu */
			} /* End if buttun pressed is the right button */
		} /* End OnButonPressEvent */
	} /* End Class ContactNodeView */
} /* End Namespace */